package framework;

import java.io.File;
import java.io.FileReader;
import java.util.Properties;
import org.apache.log4j.Logger;
import framework.helpers.AppointmentsHelper;
import framework.helpers.AuxiliaryHelper;
import framework.helpers.CasesHelper;
import framework.helpers.CleaningHelper;
import framework.helpers.NavigationHelper;
import framework.helpers.PlanningHelper;
import framework.helpers.ReferralsHelper;
import framework.helpers.VisitsHelper;
import framework.helpers.WebDriverHelper;

public class ApplicationManager
{
	private static ApplicationManager singleton;
	private Properties props;
    private static Logger log;
    
	private WebDriverHelper webDriverHelper;
	private NavigationHelper navigationHelper;
	private AuxiliaryHelper auxiliaryHelper;
	private CleaningHelper cleaningHelper;
	
	private PlanningHelper planningHelper;
	private AppointmentsHelper appointmentsHelper;
	private VisitsHelper visitsHelper;
	private ReferralsHelper referralsHelper;
	private CasesHelper casesHelper;
	
	public static ApplicationManager getInstance() {
		return getInstance(null);
	}
	
	public static ApplicationManager getInstance(String configFile) {
		if (singleton == null)
			singleton = new ApplicationManager();
		log = Logger.getLogger("framework");
		log.trace(new File(".").getAbsolutePath());
		log.trace("configFile = " + configFile);
		if (configFile != null)
			singleton.setProperties(loadProperties(configFile));			
		return singleton;
	}
	
	public void setProperties(Properties props) {
		this.props = props;
	}
	
	public String getProperty(String key) {
		return props.getProperty(key);
	}	
	
	public String getProperty(String key, String defaultValue) {
		return props.getProperty(key, defaultValue);
	}
	
	public int getIntProperty(String key) {
		return Integer.parseInt(getProperty(key));
	}	
	
	public int getIntProperty(String key, String defaultValue) {
		return Integer.parseInt(getProperty(key, defaultValue));
	}	

	private static Properties loadProperties(String configFile) {
		Properties props = new Properties();
		try {
			props.load(new FileReader(configFile));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return props;
	}
	
	public Logger getLogger() {
		return log;
	}		
	
	public void stop() {
		if (webDriverHelper != null)
			webDriverHelper.stop();
	}
	
	public WebDriverHelper getWebDriverHelper() {
		if (webDriverHelper == null)
			webDriverHelper = new WebDriverHelper(this);
		return webDriverHelper;
	}

	public NavigationHelper getNavigationHelper() {
		if (navigationHelper == null)
			navigationHelper = new NavigationHelper(this);
		return navigationHelper;
	}

	public AuxiliaryHelper getAuxiliaryHelper() {
		if (auxiliaryHelper == null)
			auxiliaryHelper = new AuxiliaryHelper(this);
		return auxiliaryHelper;
	}
	
	public CleaningHelper getCleaningHelper() {
		if (cleaningHelper == null)
			cleaningHelper = new CleaningHelper(this);
		return cleaningHelper;
	}	
	
	public PlanningHelper getPlanningHelper() {
		if (planningHelper == null)
			planningHelper = new PlanningHelper(this);
		return planningHelper;
	}
	
	public AppointmentsHelper getAppointmentsHelper() {
		if (appointmentsHelper == null)
			appointmentsHelper = new AppointmentsHelper(this);
		return appointmentsHelper;
	}
	
	public VisitsHelper getVisitsHelper() {
		if (visitsHelper == null)
			visitsHelper = new VisitsHelper(this);
		return visitsHelper;
	}
	
	public ReferralsHelper getReferralsHelper() {
		if (referralsHelper == null)
			referralsHelper = new ReferralsHelper(this);
		return referralsHelper;
	}
	
	public CasesHelper getCasesHelper() {
		if (casesHelper == null)
			casesHelper = new CasesHelper(this);
		return casesHelper;
	}
}
